﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TuongLai_Schools
{
    public partial class frBuoihoc : Form
    {
        cls_DM_BuoiHoc objBuoihoc = new cls_DM_BuoiHoc();
        public frBuoihoc()
        {
            InitializeComponent();
        }

        private void frBuoihoc_Load(object sender, EventArgs e)
        {
            GetAll_Buoihoc1();
        }
        private void GetAll_Buoihoc1()
        {
            gridControl1.DataSource = objBuoihoc.GetAll_Buoihoc();
        }
        private void Save_Buoihoc()
        {
            clsDM_Buoihoc objdmbh = new clsDM_Buoihoc();
            objdmbh.TenBuoiHoc = gridView1.GetFocusedRowCellValue("TenBuoiHoc").ToString();
            objdmbh.GhiChu = gridView1.GetFocusedRowCellValue("GhiChu").ToString();
            bool blnResult = false;
            if (ucDataButton1.DataMode == DataState.Insert)
            {
                objdmbh.BuoiHocID = 1;
                objdmbh.isChangeable = false;
                blnResult = objBuoihoc.Insert(objdmbh);
            }
            else
            {
                objdmbh.BuoiHocID = (int)gridView1.GetFocusedRowCellValue("BuoiHocID");
                objdmbh.isChangeable = (bool)gridView1.GetFocusedRowCellValue("isChangeable");
                blnResult = objBuoihoc.update(objdmbh);
            }

            if (blnResult)
            {
                clsCommon.SaveSuccessfully();
                ucDataButton1.DataMode = DataState.View;
                gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
                gridView1.OptionsBehavior.Editable = false;
            }
            else
            {
                clsCommon.SaveNotSuccessfully();
                return;
            }
        }

        private void ucDataButton1_DeleteHandler()
        {
            if (clsCommon.ConfirmDeletion(gridView1.GetFocusedRowCellValue("TenBuoiHoc").ToString()) == DialogResult.No)
                return;
            objBuoihoc.delete((int)gridView1.GetFocusedRowCellValue("BuoiHocID"));
            GetAll_Buoihoc1();
        }

        private void ucDataButton1_SaveHandler()
        {
            Save_Buoihoc();
        }

        private void ucDataButton1_InsertHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            gridView1.OptionsBehavior.Editable = true;
        }

        private void ucDataButton1_EditHandler()
        {
            gridView1.OptionsBehavior.Editable = true;
        }

        private void ucDataButton1_CancelHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            gridView1.OptionsBehavior.Editable = false;
            GetAll_Buoihoc1();
        }
    }
}
